package com.yupi.springbootinit.manager;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.model.*;
import com.qcloud.cos.model.ciModel.persistence.PicOperations;
import com.yupi.springbootinit.config.CosClientConfig;
import java.io.File;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;

/**
 * Cos 对象存储操作
 *
 * @author <a href="https://github.com/liyupi">程序员鱼皮</a>
 * @from <a href="https://yupi.icu">编程导航知识星球</a>
 */
@Component
public class CosManager {

    @Resource
    private CosClientConfig cosClientConfig;

    @Resource
    private COSClient cosClient;


    /**
     * 上传对象
     *
     * @param key 唯一键
     * @param localFilePath 本地文件路径
     * @return
     */
    public PutObjectResult putObject(String key, String localFilePath) {
        PutObjectRequest putObjectRequest = new PutObjectRequest(cosClientConfig.getBucket(), key,
                new File(localFilePath));
        return cosClient.putObject(putObjectRequest);
    }

    /**
     * 上传对象
     *
     * @param key 唯一键
     * @param file 文件
     * @return
     */
    public PutObjectResult putObject(String key, File file) {
        PutObjectRequest putObjectRequest = new PutObjectRequest(cosClientConfig.getBucket(), key,
                file);
        return cosClient.putObject(putObjectRequest);
    }


    /**
     * 上传对象
     *
     * @param key  唯一键
     * @param file 文件
     * @return
     */
    /**
     * 上传对象（附带图片信息）
     *
     * @param key  唯一键
     * @param file 文件
     */
    public PutObjectResult putPictureObject(String key, File file) {
        PutObjectRequest putObjectRequest = new PutObjectRequest(cosClientConfig.getBucket(), key,
                file);
        // 对图片进行处理（获取基本信息也被视作为一种处理）
        PicOperations picOperations = new PicOperations();
        // 1 表示返回原图信息
        picOperations.setIsPicInfo(1);
        // 构造处理参数
        putObjectRequest.setPicOperations(picOperations);
        return cosClient.putObject(putObjectRequest);
    }


    /**
     * 图片压缩上传
     */
//    public PutObjectResult putPictureObject(String key, File file) {
//        PutObjectRequest putObjectRequest = new PutObjectRequest(cosClientConfig.getBucket(), key,
//                file);
//        // 对图片进行处理（获取基本信息也被视作为一种处理）
//        PicOperations picOperations = new PicOperations();
//        // 1 表示返回原图信息
//        picOperations.setIsPicInfo(1);
//        List<PicOperations.Rule> rules = new ArrayList<>();
//        // 图片压缩（转成 webp 格式）
//        String webpKey = FileUtil.mainName(key) + ".webp";
//        PicOperations.Rule compressRule = new PicOperations.Rule();
//        compressRule.setRule("imageMogr2/format/webp");
//        compressRule.setBucket(cosClientConfig.getBucket());
//        compressRule.setFileId(webpKey);
//        rules.add(compressRule);
//        // 构造处理参数
//        picOperations.setRules(rules);
//        putObjectRequest.setPicOperations(picOperations);
//        return cosClient.putObject(putObjectRequest);
//    }


    /**
     * 下载对象
     *
     * @param key 唯一键
     */
    public COSObject getObject(String key) {
        GetObjectRequest getObjectRequest = new GetObjectRequest(cosClientConfig.getBucket(), key);
        return cosClient.getObject(getObjectRequest);
    }

    public void deleteObject(String key) {
        DeleteObjectRequest request = new DeleteObjectRequest(cosClientConfig.getBucket(), key);
        cosClient.deleteObject(request);
    }
}
